package com.idelan.commonMessage.controller;

import com.idelan.commonMessage.constant.ResponseCode;
import com.idelan.commonMessage.constant.RestApi;
import com.idelan.commonMessage.model.JPushParameter;
import com.idelan.commonMessage.utils.JpushClientUtil;
import io.swagger.annotations.Api;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* @Description:    java类作用描述
* @Author:         jzhou
* @CreateDate:     2018\7\18 0018 15:46
* @UpdateUser:     jzhou
* @UpdateDate:     2018\7\18 0018 15:46
* @UpdateRemark:   修改内容
* @Version:        1.0
*/
@RestController
@RequestMapping(value = "/jpush")
@Api("极光推送")
public class JPushController {
    @PostMapping(value="/sendToAll")
    public RestApi sendAll(@Valid JPushParameter jPushParameter, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return jPushParameter != null ? RestApi.getSuccessApi(
                JpushClientUtil.sendToAll(
                jPushParameter.getNotification_title(),
                jPushParameter.getMsg_title(),
                jPushParameter.getMsg_content(),
                jPushParameter.getExtrasparam())) : null;
    }

    @PostMapping(value = "/sendToAliasId")
    public RestApi sendToAliasId(@Valid JPushParameter jPushParameter, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return jPushParameter != null  && ""!=jPushParameter.getAliasId() ? RestApi.getSuccessApi(
                JpushClientUtil.sendToAliasId(jPushParameter.getAliasId(),
                jPushParameter.getNotification_title(),
                jPushParameter.getMsg_title(),
                jPushParameter.getMsg_content(),
                jPushParameter.getExtrasParmMap())) : null;
    }
    @PostMapping(value = "/sendToRegistrationId")
    public RestApi sendToRegistrationId(@Valid JPushParameter jPushParameter, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return jPushParameter != null && ""!=jPushParameter.getRegistrationId()? RestApi.getSuccessApi(
                JpushClientUtil.sendToRegistrationId(
                jPushParameter.getRegistrationId(),
                jPushParameter.getNotification_title(),
                jPushParameter.getMsg_title(),
                jPushParameter.getMsg_content(),
                jPushParameter.getExtrasparam())): null;
    }
    @PostMapping(value = "/sendToAllAndroid")
    public RestApi sendToAllAndroid(@Valid JPushParameter jPushParameter, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return jPushParameter != null ? RestApi.getSuccessApi(
                JpushClientUtil.sendToAllAndroid(
                jPushParameter.getNotification_title(),
                jPushParameter.getMsg_title(),
                jPushParameter.getMsg_content(),
                jPushParameter.getExtrasparam())): null;
    }
    @PostMapping(value = "/sendToAllIos")
    public RestApi sendToAllIos(@Valid JPushParameter jPushParameter, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestApi.getErrorApi(ResponseCode.RES_PARAMETER_INVALID, bindingResult.getFieldError().getDefaultMessage());
        }
        return jPushParameter != null ? RestApi.getSuccessApi(
                JpushClientUtil.sendToAllIos(
                jPushParameter.getNotification_title(),
                jPushParameter.getMsg_title(),
                jPushParameter.getMsg_content(),
                jPushParameter.getExtrasparam())): null;
    }
}
